#include "dlg_commmon.h"
#include "ui_dlg_commmon.h"
#include"dlg_ModiftyStu_LearnExp.h"
#include"QTableWidget"
#include<QDebug>
#include<QMessageBox>




dlg_commmon::dlg_commmon(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::dlg_commmon)
{
    ui->setupUi(this);
    this->m_sql=new supsql;
    m_sql->init();
    //钟汇鸿：将格式设置转移到构造函数
    ui->tableWidget->verticalHeader()->hide();//取消行号
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40); //设置行高
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //均分各列
    //钟汇鸿：将修改命令的函数连接转移到构造函数
    connect(ui->tableWidget,&QTableWidget::itemChanged,[=](QTableWidgetItem *item)
    {
        if(!item||!info)
            return;
        int row = item->row();
        int col = item->column();
        int keycol=info->get_key_col();
        qDebug()<<"修改的行和列是"<<row<<" "<<col;
        m_sql->UpdateInfo(info->getTablename(),info->getcolname(col),item->text(),info->getcolname(keycol),ui->tableWidget->item(row,keycol)->text());
    });

}

dlg_commmon::~dlg_commmon()
{
    delete ui;
}

void dlg_commmon::fun0(InfoSet infos)//钟汇鸿：使用了通用函数
{
    info=nullptr;//用于控制修改函数不启用
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(infos.colsize);
    ui->tableWidget->setHorizontalHeaderLabels(infos.HorizontalHeaderLabels);
    ui->tableWidget->setRowCount(infos.size);
    for(int i=0;i<infos.size;i++)
    {
        for(int j=0;j<infos.info->colsize();j++)
        {
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(infos.item(i,j)));
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        }
    }
    info=infos.info;//记录修改的类型信息
    m_sql->clearCommand();
}

//学生基本信息
void dlg_commmon::fun1(StuInfo info)//由于使用了已删除的value函数，这个函数弃用。
{
//    //陈嘉俊 设置状态
//    State=1;


//    ui->tableWidget->verticalHeader()->hide();//取消行号
//    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
//    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40); //设置行高
//    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //均分各列

//    ui->tableWidget->clear();
//    ui->tableWidget->setColumnCount(4);
//    QStringList l;
//    l<<"学生地址"<<"学生电话号码"<<"学生编号"<<"学生姓名";//更改了学生查询的列，以便与数据库字段一致。
//    ui->tableWidget->setHorizontalHeaderLabels(l);
//    ui->tableWidget->setRowCount(1);
//    for (int i=0;i<4;i++)
//    {
//        ui->tableWidget->setItem(0,i,new QTableWidgetItem(info.value(i)));
//        ui->tableWidget->item(0,i)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
//    }
//    //陈嘉俊 修改用
//    //钟汇鸿：将修改用的代码放到一起
//    m_stu.stu_address=info.stu_address;
//    m_stu.stu_id=info.stu_id;
//    m_stu.stu_name=info.stu_name;
//    m_stu.stu_password=info.stu_password;
//    m_stu.stu_phonenum=info.stu_phonenum;
//    m_stu.stu_username=info.stu_username;
//    connect(ui->tableWidget,&QTableWidget::itemChanged,[=](QTableWidgetItem *item)
//    {
//        if(!item)
//            return;
//        int row = item->row();
//        int col = item->column();
//        qDebug()<<row<<" "<<col;
//        //陈嘉俊 记录修改的数据，在修改的按钮中决定要不要写入数据库
//        //钟汇鸿：修改了m_stu的写入逻辑。由于列的改变，原本的代码已经无用。
//        m_stu.value(item->column())=item->text();
//    });


}

//教师基本信息
void dlg_commmon::fun2(TechInfo info)
{
    //设置状态
    State=2;

    ui->tableWidget->verticalHeader()->hide();//取消行号
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40); //设置行高
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //均分各列
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(5);

    m_tech.tech_address=info.tech_address;
    m_tech.tech_id=info.tech_id;
    m_tech.tech_name=info.tech_name;
    m_tech.tech_password=info.tech_password;
    m_tech.tech_phone=info.tech_phone;
    m_tech.tech_school=info.tech_school;\
    m_tech.tech_username=info.tech_username;


    QStringList l;
    l<<"教师编号"<<"教师姓名"<<"教学学校"<<"教师地址"<<"教师电话号码";
    ui->tableWidget->setHorizontalHeaderLabels(l);
    ui->tableWidget->setRowCount(1);
    ui->tableWidget->setItem(0,0,new QTableWidgetItem(info.tech_id));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem(info.tech_name));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem(info.tech_school));
    ui->tableWidget->setItem(0,3,new QTableWidgetItem(info.tech_address));
    ui->tableWidget->setItem(0,4,new QTableWidgetItem(info.tech_phone));

    for (int j=0;j<5;j++)
    {
        ui->tableWidget->item(0,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    }

    //陈嘉俊  修改用
    connect(ui->tableWidget,&QTableWidget::itemChanged,[=](QTableWidgetItem *item)
    {
        if(!item)
        return;
        int row = item->row();
        int col = item->column();
        qDebug()<<row<<" "<<col;
        //陈嘉俊 记录修改的数据，在修改的按钮中决定要不要写入数据库
        switch(item->column())
        {
          case 1:
            m_tech.tech_name=item->text();
              break;
          case 2:
            m_tech.tech_school=item->text();
            break;
          case 3:
            m_tech.tech_address=item->text();
          break;
        case 4:
           m_tech.tech_phone=item->text();
          break;
          default:
            break;
        }
    });
}

//学生学习经历
void dlg_commmon::fun3(StuExp info)
{
    //陈嘉俊 按照fun3函数编号
    State=3;
    ui->tableWidget->verticalHeader()->hide();//取消行号
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40); //设置行高
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //均分各列

    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(5);
    QStringList l;
    l<<"学生编号"<<"小学"<<"初中"<<"高中"<<"大学";
    ui->tableWidget->setHorizontalHeaderLabels(l);
    ui->tableWidget->setRowCount(1);
    ui->tableWidget->setItem(0,0,new QTableWidgetItem(info.stu_id));
    m_StuExp.stu_id=info.stu_id;
    ui->tableWidget->setItem(0,1,new QTableWidgetItem(info.primary_school));
    m_StuExp.primary_school=info.primary_school;
    ui->tableWidget->setItem(0,2,new QTableWidgetItem(info.middle_school));
    m_StuExp.middle_school=info.middle_school;
    ui->tableWidget->setItem(0,3,new QTableWidgetItem(info.high_school));
    m_StuExp.high_school=info.high_school;
    ui->tableWidget->setItem(0,4,new QTableWidgetItem(info.university));
    m_StuExp.university=info.university;
    for (int j=0;j<5;j++)
    {
        ui->tableWidget->item(0,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    }


    connect(ui->tableWidget,&QTableWidget::itemChanged,[=](QTableWidgetItem *item){
    if(!item)
    return;
    int row = item->row();
    int col = item->column();
    qDebug()<<row<<" "<<col;
    //陈嘉俊 记录修改的数据，在修改的按钮中决定要不要写入数据库
    switch(item->column())
    {
      case 1:
        m_StuExp.primary_school=item->text();
        qDebug()<<m_StuExp.primary_school;
          break;
      case 2:
        m_StuExp.middle_school=item->text();
        qDebug()<<m_StuExp.middle_school;
        break;
      case 3:
        m_StuExp.high_school=item->text();
      qDebug()<<m_StuExp.high_school;
      break;
    case 4:
       m_StuExp.university=item->text();
      qDebug()<<m_StuExp.university;
      break;
      default:
        break;
    }
    });

}



void dlg_commmon::fun5(QList<Sci_Result> list)
{
    ui->tableWidget->verticalHeader()->hide();//取消行号
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40); //设置行高
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //均分各列

    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(3);
    QStringList temp;
    temp<<"教师编号"<<"科研成果编号"<<"科研成果标题";
    ui->tableWidget->setHorizontalHeaderLabels(temp);
    ui->tableWidget->setRowCount(list.size());
    for(int i=0;i<list.size();i++)
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(list[i].tch_id));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(list[i].sci_result_id));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(list[i].sci_result_name));
    }
}

void dlg_commmon::fun6(QList<Work_exp> we)
{
    qDebug()<<"进入fun6"<<we.size()<<endl;
    ui->tableWidget->verticalHeader()->hide();//取消行号
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40); //设置行高
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //均分各列

    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(5);
    QStringList temp;
    temp<<"教师编号"<<"工作编号"<<"开始时间"<<"结束时间"<<"工作地点";
    ui->tableWidget->setHorizontalHeaderLabels(temp);
    ui->tableWidget->setRowCount(we.size());
    for(int i=0;i<we.size();i++)
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(we[i].tch_id));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(we[i].tch_work_id));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(we[i].start_time));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(we[i].end_time));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(we[i].work_place));
    }

}

void dlg_commmon::fun7(QList<Develop> dev)
{
    ui->tableWidget->verticalHeader()->hide();//取消行号
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40); //设置行高
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //均分各列

    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(4);
    QStringList temp;
    temp<<"教师编号"<<"教学现状"<<"成长目标"<<"提升方法";
    ui->tableWidget->setHorizontalHeaderLabels(temp);
    ui->tableWidget->setRowCount(dev.size());
    for(int i=0;i<dev.size();i++)
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(dev[i].tch_id));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(dev[i].now_condition));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(dev[i].grow_up));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(dev[i].dev_method));
    }
}

void dlg_commmon::fun8(QList<Recruit> re)
{
    ui->tableWidget->verticalHeader()->hide();//取消行号
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40); //设置行高
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //均分各列

    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(3);
    QStringList temp;
    temp<<"招聘信息编号"<<"招聘岗位"<<"预期薪资";
    ui->tableWidget->setHorizontalHeaderLabels(temp);
    ui->tableWidget->setRowCount(re.size());
    for(int i=0;i<re.size();i++)
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(re[i].recruit_id));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(re[i].pos));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(re[i].salary));
    }
}

void dlg_commmon::fun9(QList<Appc> ap)
{
    ui->tableWidget->verticalHeader()->hide();//取消行号
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40); //设置行高
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //均分各列

    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(4);
    QStringList temp;
    temp<<"应聘信息编号"<<"应聘老师编号"<<"应聘岗位"<<"薪资";
    ui->tableWidget->setHorizontalHeaderLabels(temp);
    ui->tableWidget->setRowCount(ap.size());
    for(int i=0;i<ap.size();i++)
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(ap[i].app_id));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(ap[i].tech_id));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(ap[i].pos));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(ap[i].salary));
    }
}

void dlg_commmon::fun10(QList<Intern> itn)
{
    ui->tableWidget->verticalHeader()->hide();//取消行号
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);//表头字体居中
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(40); //设置行高
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //均分各列

    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(3);
    QStringList temp;
    temp<<"实习教师编号"<<"实习编号"<<"实习岗位";
    ui->tableWidget->setHorizontalHeaderLabels(temp);
    ui->tableWidget->setRowCount(itn.size());
    for(int i=0;i<itn.size();i++)
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(itn[i].tech_id));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(itn[i].intern_id));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(itn[i].intern_name));
    }
}

void dlg_commmon::on_Mod_pushButton_clicked()
{
    QMessageBox msgBox;
    msgBox.setText("你确定要进行修改吗?");
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);
    if(msgBox.exec()==QMessageBox::Yes){
        m_sql->executeDelayedCommand();
        return;
    }//在此处的return，相当于注释以下代码
    if(msgBox.exec()==QMessageBox::Yes){
  //陈嘉俊 用switch来切换不同页面的修改
    switch (State) {
    case 1:
         if(m_sql->UpdateStuInfo(m_stu))
        {
         QMessageBox::information(this,"提示","修改成功");
        }
        else
        {
         QMessageBox::information(this,"提示","修改失败");
        }
    break;
        case 2:
        if(m_sql->changeTechInfo(m_tech))
       {
        QMessageBox::information(this,"提示","修改成功");
       }
       else
       {
        QMessageBox::information(this,"提示","修改失败");
       }
        break;
    case 3:
        if(m_sql->UpdateStuExp(this->m_StuExp))
        {
            QMessageBox::information(this,"提示","修改成功");
        }
        else{
            QMessageBox::information(this,"提示","修改失败");
        }
        break;
    default:
        break;
    }
    }
    else
    {
        qDebug()<<"你是sb吗，不该搞什么";
    }


}
